package com.android.juliomedina.DB;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Thoughts_DBHelperFeed {

	public static final String TAG = "DBAdapter";
	public static final String DATABASE_NAME = "thofeed.db";
	public static final int DATABASE_VERSION = 1;
	public static final String DATABASE_TABLE_FEED = "thoughts";
	public static final String DESCRIPTION = "description";
	public static final String TITLE = "title";
	public static final String KEY_ID = "_id";
	public static final String PUB_DATE = "date";
	public static final String SELECT_FROM = null;
	private Context context = null;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;
	public SQLiteDatabase getReadableData;
	private static final String DATABASE_CREATE_FEED = "CREATE TABLE "
			+ DATABASE_TABLE_FEED + " ( " + KEY_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT," + TITLE + " TEXT UNIQUE , "
			+ DESCRIPTION + " TEXT UNIQUE , " + PUB_DATE + " TEXT " + ");";

	public Thoughts_DBHelperFeed(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			System.out.println("in on create...................");
			db.execSQL(DATABASE_CREATE_FEED);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS users");
			onCreate(db);
		}
	}

	public void open() throws SQLException {
		db = DBHelper.getWritableDatabase();
	}

	public void close() {
		DBHelper.close();
	}

	public long AddFeed(String title, String desc, String date) {
		ContentValues values = new ContentValues();
		values.put(TITLE, title);
		values.put(DESCRIPTION, desc);
		values.put(PUB_DATE, date);
		return db.insert(DATABASE_TABLE_FEED, null, values);

	}

}
